#!/bin/sh

update-alternatives --install %OPTWARE_TARGET_PREFIX%/bin/scp scp %OPTWARE_TARGET_PREFIX%/bin/openssh-scp 80
update-alternatives --install %OPTWARE_TARGET_PREFIX%/bin/ssh ssh %OPTWARE_TARGET_PREFIX%/bin/openssh-ssh 80

echo

# Check for the SSH1 RSA key
if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_key ]; then
    echo Generating RSA Key...
    %OPTWARE_TARGET_PREFIX%/bin/ssh-keygen -t rsa1 -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_key -C '' -N ''
fi

# Check for the SSH2 RSA key
if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_rsa_key ]; then
    echo Generating RSA Key...
    %OPTWARE_TARGET_PREFIX%/bin/ssh-keygen -t rsa -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_rsa_key -C '' -N ''
fi

# Check for the SSH2 DSA key
if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_dsa_key ]; then
    echo Generating DSA Key...
    %OPTWARE_TARGET_PREFIX%/bin/ssh-keygen -t dsa -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_dsa_key -C '' -N ''
fi

# Check for the SSH2 ECDSA key
if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_ecdsa_key ]; then
    echo Generating ECDSA Key...
    %OPTWARE_TARGET_PREFIX%/bin/ssh-keygen -t ecdsa -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_ecdsa_key -C '' -N ''
fi

# Check for the SSH2 ED25519 key
if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_ed25519_key ]; then
    echo Generating ED25519 Key...
    %OPTWARE_TARGET_PREFIX%/bin/ssh-keygen -t ed25519 -f %OPTWARE_TARGET_PREFIX%/etc/openssh/ssh_host_ed25519_key -C '' -N ''
fi

if [ ! -d /root -a -w / ];  then
    mkdir /root
fi

# Some devices do not have a nobody user for priviledge separation
if grep -v nobody /etc/passwd > /dev/null ; then
    sed -i -e 's/\#UsePrivilegeSeparation yes/UsePrivilegeSeparation no/' %OPTWARE_TARGET_PREFIX%/etc/openssh/sshd_config
fi

echo Fixing permissions on the /tmp directory...
chmod 1777 /tmp

%OPTWARE_TARGET_PREFIX%/etc/init.d/S40sshd
